﻿using Microsoft.Win32;
using SvgViewer.Entities;
using SvgViewer.ViewModel;
using System;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SvgViewer.Core;
using SvgViewer.Utilities;

namespace SvgViewer.Views;

/// <summary>
/// ExportView.xaml 的交互逻辑
/// </summary>
public partial class ExportView : IViewService
{
    private ExportViewModel model;
    private readonly Regex           re1 = new("[^0-9]+");
    private readonly Regex           re2 = new("[^0-9.-]+");

    /// <summary>
    /// 构造
    /// </summary>
    public ExportView()
    {
        InitializeComponent();
        InitEvents();
    }

    private void InitEvents()
    {
        txtSize.PreviewTextInput             += NumberText1PreviewKeyDown;
        txtPadding.PreviewTextInput          += NumberText1PreviewKeyDown;
        txtRotate.PreviewTextInput           += NumberText1PreviewKeyDown;
        txtMarginLeft.PreviewTextInput       += NumberText2PreviewKeyDown;
        txtMarginTop.PreviewTextInput        += NumberText2PreviewKeyDown;
        txtMarginRight.PreviewTextInput      += NumberText2PreviewKeyDown;
        txtMarginBottom.PreviewTextInput     += NumberText2PreviewKeyDown;
        txtBackgroundRotate.PreviewTextInput += NumberText2PreviewKeyDown;

        txtSize.PreviewMouseUp             += TextBoxPreviewMouseUp;
        txtPadding.PreviewMouseUp          += TextBoxPreviewMouseUp;
        txtRotate.PreviewMouseUp           += TextBoxPreviewMouseUp;
        txtMarginLeft.PreviewMouseUp       += TextBoxPreviewMouseUp;
        txtMarginTop.PreviewMouseUp        += TextBoxPreviewMouseUp;
        txtMarginRight.PreviewMouseUp      += TextBoxPreviewMouseUp;
        txtMarginBottom.PreviewMouseUp     += TextBoxPreviewMouseUp;
        txtBackgroundRotate.PreviewMouseUp += TextBoxPreviewMouseUp;

        this.ContentRendered += ExportViewContentRendered;
    }

    private void ExportViewContentRendered(object sender, EventArgs e)
    {
        this.WindowState  = WindowState.Maximized;
        model             = (ExportViewModel)this.DataContext;
        model.ViewService = this;
        if (model != null)
        {
            model.Property.PropertyChanged += ModelPropertyChanged;
        }
    }

    private void ModelPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(ExportProperty.SelectedSizeIndex))
        {
            if (model.Property.SelectedSizeIndex == 7)
            {
                txtSize.Focus();
                txtSize.SelectAll();
            }
        }
    }

    private void TextBoxPreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (sender is TextBox text)
        {
            text.Focus();
            text.SelectAll();
        }
    }

    private void NumberText1PreviewKeyDown(object sender, TextCompositionEventArgs e)
    {
        e.Handled = re1.IsMatch(e.Text);
    }

    private void NumberText2PreviewKeyDown(object sender, TextCompositionEventArgs e)
    {
        e.Handled = re2.IsMatch(e.Text);
    }

    void IViewService.Show(string token, params object[] args)
    {
        switch (token)
        {
            case "export":
                ExportDialog();
                break;
        }
    }

    void IViewService.ShowTip(string message)
    {
        Snackbar.MessageQueue?.Enqueue(message, null, null, null, false, true, TimeSpan.FromSeconds(3));
    }

    private void ExportDialog()
    {
        var dialog = new SaveFileDialog {
            Filter = "Icon|*.ico|PNG|*.png|Bitmap|*.bmp|Jpeg|*.jpeg|Gif|*.gif|Icon|*.ico|Tiff|*.tiff"
        };
        var result = dialog.ShowDialog(this);
        if (result != true)
        {
            return;
        }

        try
        {
            if (model.CodeMode)
            {
                ImageUtility.SaveImage(model.Property.CodeImage, dialog.FileName);
                //model.Property.BuildCodeImage();
            }
            else
            {
                ImageUtility.SaveImage(Grid, dialog.FileName);
            }

            ((IViewService)this).ShowTip(LanguageManager.Instance.GetText("ExportView.SaveSucceed"));
        }
        catch (Exception ex)
        {
            ((IViewService)this).ShowTip(string.Format(LanguageManager.Instance.GetText("SaveFailed"),ex.Message));
        }
    }
}